from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# 为了考试快
from .models import *
from .serializers import *
# Create your views here.
# 商品展示
class Show(APIView):
    def get(self, request):
        good = Good.objects.all()
        goods = Goodser(good,many=True)

        return Response({
            'code':200,
            'goods':goods.data
        })

# 登录页面
class Login(APIView):
    def post(self, request):
        name = request.data['name']
        password = request.data['password']
        user = User.objects.filter(name=name,password=password)
        if user:
            return Response({
                'code':200,
                'message':'登录成功'
            })

# 评价
class Datails(APIView):
    def post(self, request):

        return Response({
            'code':200,
            'message':'成功'
        })

# 评价添加
class Add_datail(APIView):
    def post(self, request):
        id = request.data['ids']
        name = request.data['name']
        detail = request.data['detail']

        good = Good.objects.get(pk=id)
        good.count += 1
        good.save()
        user = User.objects.filter(name=name).first()
        Detail.objects.create(
            uid_id=user.id,
            gid_id=id,
            count=detail
        )

        return Response({
            'code':200,
            'message':'成功'
        })

# 商品评论展示
class Showdetail(APIView):
    def get(self, request):
        id = request.GET.get('ids',None)
        ding = Ding.objects.filter(gid=id)
        dinglist = Dingser(ding, many=True)
        detail = Detail.objects.all()
        detail_list = Detailser(detail,many=True)

        # return Response({
        #
        # })
        # good = Good.objects.all()
        # goods = Goodser(good,many=True)

        return Response({
            'code':200,
            'detail_list':detail_list.data,
            'dinglist': dinglist.data,
        })
